/**
 * @author 徐楠
 * @date 2022/4/11 20:17
 * @version 1.0
 */

package com.xunan.likou;

import java.util.*;

public class LifeGame {
    public static void main(String[] args) {
        //int[][] board = {{0, 1, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0}};
        int[][] board = {{1, 1}, {1, 0}};
        gameOfLife(board);

    }

    public static void gameOfLife(int[][] board) {
        int x = board.length;
        int y = board[0].length;
        Map<Integer, List<Integer>> waitForDeathMap = new HashMap<>();
        Map<Integer, List<Integer>> waitForLifeMap = new HashMap<>();

        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                int length = 0;

                //死细胞
                if (board[i][j] == 0) {
                    if (i - 1 >= 0) {
                        if (j - 1 >= 0 && board[i - 1][j - 1] == 1) {
                            length++;
                        }
                        if (board[i - 1][j] == 1) {
                            length++;
                        }
                        if (j + 1 < y && board[i - 1][j + 1] == 1) {
                            length++;
                        }
                    }

                    if (j - 1 >= 0 && board[i][j - 1] == 1) {
                        length++;
                    }
                    if (j + 1 < y && board[i][j + 1] == 1) {
                        length++;
                    }

                    if (i + 1 < x) {
                        if (j - 1 >= 0 && board[i + 1][j - 1] == 1) {
                            length++;
                        }
                        if (board[i + 1][j] == 1) {
                            length++;
                        }
                        if (j + 1 < y && board[i + 1][j + 1] == 1) {
                            length++;
                        }
                    }

                    if (length == 3) {
                        List<Integer> list = waitForLifeMap.getOrDefault(i, new ArrayList<>());
                        list.add(j);
                        waitForLifeMap.put(i, waitForLifeMap.getOrDefault(i, list));
                    }


                } else {

                    if (i - 1 >= 0) {
                        if (j - 1 >= 0 && board[i - 1][j - 1] == 1) {
                            length++;
                        }
                        if (board[i - 1][j] == 1) {
                            length++;
                        }
                        if (j + 1 < y && board[i - 1][j + 1] == 1) {
                            length++;
                        }
                    }

                    if (j - 1 >= 0 && board[i][j - 1] == 1) {
                        length++;
                    }
                    if (j + 1 < y && board[i][j + 1] == 1) {
                        length++;
                    }

                    if (i + 1 < x) {
                        if (j - 1 >= 0 && board[i + 1][j - 1] == 1) {
                            length++;
                        }
                        if (board[i + 1][j] == 1) {
                            length++;
                        }
                        if (j + 1 < y && board[i + 1][j + 1] == 1) {
                            length++;
                        }
                    }

                    if (length < 2) {
                        List<Integer> list = waitForDeathMap.getOrDefault(i, new ArrayList<>());
                        list.add(j);
                        waitForDeathMap.put(i, waitForDeathMap.getOrDefault(i, list));
                    } else if (length == 2 || length == 3) {
                        List<Integer> list = waitForLifeMap.getOrDefault(i, new ArrayList<>());
                        list.add(j);
                        waitForLifeMap.put(i, waitForLifeMap.getOrDefault(i, list));
                    } else {
                        List<Integer> list = waitForDeathMap.getOrDefault(i, new ArrayList<>());
                        list.add(j);
                        waitForDeathMap.put(i, waitForDeathMap.getOrDefault(i, list));
                    }
                }

            }
        }


        Set<Map.Entry<Integer, List<Integer>>> waitForDeathEntries = waitForDeathMap.entrySet();
        for (Map.Entry<Integer, List<Integer>> entry : waitForDeathEntries) {
            Integer key = entry.getKey();
            List<Integer> value = entry.getValue();
            for (Integer integer : value) {
                board[key][integer] = 0;
            }
        }

        Set<Map.Entry<Integer, List<Integer>>> waitForLifeEntries = waitForLifeMap.entrySet();
        for (Map.Entry<Integer, List<Integer>> entry : waitForLifeEntries) {
            Integer key = entry.getKey();
            List<Integer> value = entry.getValue();
            for (Integer integer : value) {
                board[key][integer] = 1;
            }
        }

        System.out.println();
        for (int[] ints : board) {
            for (int anInt : ints) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
    }
}
